package com.cloud.force.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.force.api.entity.TaskList;
import com.cloud.force.api.entity.TaskListField;
import com.cloud.force.api.mapper.TaskListFieldMapper;
import com.cloud.force.api.mapper.TaskListMapper;
import com.cloud.force.api.service.TaskListConfigService;
import com.cloud.force.common.result.QkIcon;
import com.cloud.force.common.result.QkTreeNode;
import com.cloud.force.common.utils.RedisKeyHelper;
import com.cloud.force.common.utils.SqlParserHelper;
import com.cloud.force.common.utils.TreeHelper;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class TaskListConfigServiceImpl implements TaskListConfigService {

	@Autowired
	private TaskListMapper taskListMapper;

	@Autowired
	private TaskListFieldMapper taskListFieldMapper;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public QkTreeNode[] listTaskListTree(int bizCode) {
		Example example = new Example(TaskList.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("bizCode", bizCode);
		example.orderBy("displayOrder");
		List<TaskList> taskLists = taskListMapper.selectByExample(example);
		List<QkTreeNode> nodes = new ArrayList<QkTreeNode>();
		for (TaskList taskList : taskLists) {
			QkTreeNode taskListNode = new QkTreeNode(taskList.getTaskListId(), taskList.getTaskListName());
			taskListNode.setIcon(QkIcon.FA_LAPTOP);
			taskListNode.setSelectable(true);
			taskListNode.setLeaf(true);
			taskListNode.setRootFlag(true);
			nodes.add(taskListNode);
		}
		return TreeHelper.buildList2Tree(nodes);
	}

	@Override
	public TaskList showDetail(long taskListId) {
		return taskListMapper.selectByPrimaryKey(taskListId);
	}

	@Override
	public Long saveUpdateTaskList(TaskList taskList) {
		// SQL语法校验
		List<TaskListField> fields = SqlParserHelper.listFieldInfoBySQL(taskList.getTaskListSql(), jdbcTemplate);
		// 保存案卷箱子
		Long taskListId = taskList.getTaskListId();
		if (null == taskListId || taskListId.intValue() == 0) {
			taskListId = taskList.getTaskListId();
			taskListMapper.insert(taskList);
			for (TaskListField field : fields) {
				field.setTaskListId(taskListId);
				field.setFieldWidth(200);
				taskListFieldMapper.insert(field);
			}
		} else {
			TaskList tl = taskListMapper.selectByPrimaryKey(taskListId);
			if (StringUtils.isEmpty(tl.getTaskListSql()) || !tl.getTaskListSql().equals(taskList.getTaskListSql())) {
				// SQL修改，需重新生成字段
				List<String> fieldNames = new ArrayList<String>();
				List<TaskListField> needInfer = new ArrayList<TaskListField>();
				for (TaskListField f : fields) {
					fieldNames.add(f.getColumnName());
					f.setTaskListId(taskListId);
					if (!taskListFieldMapper.existsWithPrimaryKey(f)) {
						f.setFieldWidth(200);
						taskListFieldMapper.insert(f);
						needInfer.add(f);
					}
				}
				// 刪除減少的列
				Example fieldsExample = new Example(TaskListField.class);
				Example.Criteria criteria = fieldsExample.createCriteria();
				criteria.andEqualTo("taskListId", taskListId);
				criteria.andNotIn("columnName", fieldNames);
				taskListFieldMapper.deleteByExample(fieldsExample);

				// TODO:根据已经配置的数据推断新增加的列的各种情况信息
				if (needInfer.size() > 0) {

				}
			}

			taskListMapper.updateByPrimaryKey(taskList);
		}
		return taskListId;
	}

	@Override
	public Long delete(long taskListId) {
		Example tlfExample = new Example(TaskListField.class);
		Example.Criteria tlfCriteria = tlfExample.createCriteria();
		tlfCriteria.andEqualTo("taskListId", taskListId);
		taskListFieldMapper.deleteByExample(tlfExample);
		taskListMapper.deleteByPrimaryKey(taskListId);
		return taskListId;
	}

}
